package br.com.hsp.system.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.hsp.organizacao.Instituicao;

public class InstituicaoRepository {
	
	private EntityManager manager ;
	
	public InstituicaoRepository ( EntityManager manager ) {
		this.manager = manager ;
	}
	
	public void add(Instituicao instituicao) {
		this.manager.persist(instituicao);
	}
	
	public void remove(Long id) {
		Instituicao instituicao = this.procura(id);
		this.manager.remove(instituicao);
	}
	
	public Instituicao atualiza(Instituicao instituicao) {
		return this.manager.merge(instituicao);
	}
	
	public Instituicao procura(Long id) {
		return this.manager.find(Instituicao.class, id);
	}
	
	public List<Instituicao> getLista() {
		Query query = this.manager.createQuery("SELECT x from Instituicao x");
		return query.getResultList();
	}
	
	public List<Instituicao> getListaClinicas() {
		Query query = this.manager.createQuery("SELECT x from Instituicao x WHERE x.tipo='clinica'");
		return query.getResultList();
	}
	
	public List<Instituicao> getClinica(String nome) {
		Query query = this.manager.createQuery("SELECT x from Instituicao x WHERE x.nome='"+ nome +"'");
		return query.getResultList();
	}
	
	public List<Instituicao> getListaLaboratorios() {
		Query query = this.manager.createQuery("SELECT x from Instituicao x WHERE x.tipo='laboratorio'");
		return query.getResultList();
	}
}
